package com.jin.webvideo.util.base;

import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class URLUtil {
    public static final char CONNECTOR = '=';
    public static final char SEPARTOR = '&';
    public static String format(String pattern, String... params){
        StringBuilder builder = new StringBuilder();
        char[] chars = pattern.toCharArray();
        boolean open = false;
        int index = 0;
        for (char item : chars){
            if (item == '%'){
                if (open && index < params.length){     //开启且不越界
                    builder.append(params[index]);
                    index ++;
                }
                open = !open;           //切换开闭状态
            }
            else if (!open){
                builder.append(item);
            }
        }
        return builder.toString();
    }

    public static URI resolve(String url, String baseUrl){
        URI baseUri = URI.create(baseUrl);
        URI r = baseUri.resolve(url);
        return r;
    }
    public static String resolveStr(String url,String baseUrl){
        return resolve(url, baseUrl).toASCIIString();
    }
    public static String createURL(String baseUrl, Map<String,String> params){
        StringBuilder builder = new StringBuilder();
        for (String key : params.keySet()){
            builder.append(URLUtil.SEPARTOR);
            builder.append(key);
            builder.append(URLUtil.CONNECTOR);
            builder.append(params.getOrDefault(key, ""));
        }
        String param = builder.toString();
        if (!StringUtil.isEmpty(baseUrl)){
            int index = baseUrl.indexOf("?");
            if (index == -1 && param.length()>0){
                baseUrl = baseUrl + "?";
            }
        }
        if (param.length() > 0 && baseUrl.endsWith("?")){
            param = param.substring(1);
        }
        return baseUrl + param;
    }
    public static Map<String,String> query2Map(String queryStr){
        Map<String,String> map = new HashMap<>();
        String[] kvArr = queryStr.split(String.valueOf(URLUtil.SEPARTOR) );
        for (String item : kvArr){
            int index = item.indexOf(URLUtil.CONNECTOR);
            if (index == -1){
                continue;
            }
            String key = item.substring(0, index);
            String val = item.substring(index + 1);
            map.put(key, val);
        }
        return map;
    }
    public static String encodeURL(String url) throws UnsupportedEncodingException {
        return URLEncoder.encode(url, "utf-8");
    }
    public static String decodeURL(String url) throws UnsupportedEncodingException {
        return URLDecoder.decode(url, "utf-8");
    }


    public static boolean isMp4(String url){
        return chkFileType(url,"mp4");
    }

    public static boolean chkFileType(String url, String fileType){
        fileType = fileType.toLowerCase(Locale.ROOT);
        if (fileType(url).toLowerCase(Locale.ROOT).endsWith(fileType)){
            return true;
        }
        return false;
    }
    public static String fileType(String url){
        String baseUrl = urlNoProtocol(url);
        int index = url.toLowerCase(Locale.ROOT).indexOf("?");
        if (index != -1){
            baseUrl = url.substring(0, index);
        }
        index = baseUrl.lastIndexOf("/");
        if (index != -1){
            if (index == baseUrl.length()){
                return "";
            }
            baseUrl = baseUrl.substring(index);
        }
        index = baseUrl.lastIndexOf(".");
        if (index != -1 && index < baseUrl.length()){
            return baseUrl.substring(index + 1);
        }
        return "";
    }
    public static String https(String url){
        return "https:" + urlNoProtocol(url);
    }
    public static String http(String url){
        return "http:" + urlNoProtocol(url);
    }
    public static String urlNoProtocol(String url){
        int index = url.indexOf(":");
        if (index != -1){
            String p = url.substring(0, index).toLowerCase(Locale.ROOT);
            if (p.equals("http")|| p.equals("https")){
                url = url.substring(index+1);
            }
        }
        return url;
    }
    public static String host(String url){
        String host = "";
        try {
            host = new URL( url ).getHost();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return host;
    }
}
